<!DOCTYPE>
<html>
  <head>
    <meta charset="utf-8">
    <title>task 06</title>
    <style type="text/css">
        * {
            padding: 0;
            margin: 0;
            list-style: none;
            box-sizing: border-box;
        }
        .modal-mask {
            position: relative;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, .4);
        }
        .modal-mask .modal-wrapper {
            position: absolute;
            top: 50%;
            left: 50%;
            width: 500px;
            height: 300px;
            margin-left: -250px;
            margin-top: -150px;
        }
        .modal-wrapper .modal-title {
            position: absolute;
            top: 0px;
            left: 0px;
            width: 100%;
            font-size: 30px;
            line-height: 30px;
            height: 50px;
            padding: 10px;
            color: #fff;
            text-indent: 20px;
            font-weight: bold;
            background: rgba(0, 0, 0, .6);
            border: 1px solid #000;
        }
        .modal-wrapper .modal-body {
            width: 100%;
            height: 100%;
            background: #fff;
            color: #000;
            padding: 70px 10px;
        }
        .modal-wrapper .modal-foot {
            position: absolute;
            bottom: 0px;
            left: 0px;
            width: 100%;
            height: 50px;
            background: #fff;
            border-top: 1px solid #000;
        }
        .modal-foot #closeBtn {
            cursor: pointer;
            float: right;
            height: 30px;
            padding: 0px 15px;
            margin: 10px 20px 10px 0px;
        }
        .modal-foot #cancelBtn {
            cursor: pointer;
            float: right;
            height: 30px;
            padding: 0px 15px;
            margin: 10px 20px 10px 0px;
        }
    </style>
  </head>
<body>
    <!-- modal 生成函数 -->
	<script type="text/javascript">
        function modal(setting) {
            var title = setting.title || "",
                content = setting.content || "",
                width = setting.width || 500,
                height = setting.height || 300;
            // 将节点隐藏,添加到页面上
            var html = [
                '<div class="modal-wrapper">',
                '<div class="modal-title">' + setting.title + '</div>',
                '<div class="modal-body">' + setting.content + '</div>',
                '<div class="modal-foot">',
                '<button id="cancelBtn">取消</button>',
                '<button id="closeBtn">确定</button>',
                '</div></div>'
            ];
                html = html.join("").replace(/,/igm, "");
            var node = document.createElement("div");
                node.className = "modal-mask",
                node.style.display = "none",
                node.innerHTML = html;
            document.getElementsByTagName("body")[0].appendChild(node);
            node.querySelectorAll(".modal-wrapper")[0].style.marginLeft = "-" + width/2 + "px",
            node.querySelectorAll(".modal-wrapper")[0].style.marginTop = "-" + height/2 + "px",
            node.querySelectorAll(".modal-wrapper")[0].style.width = width,
            node.querySelectorAll(".modal-wrapper")[0].style.height = height,
            // 添加后显示
            node.style.display = "block";

            var cancelBtn = document.getElementById("cancelBtn"),
                closeBtn = document.getElementById("closeBtn");
            
            // 初始化通用方法
            this.hide = function() {
                node.style.display = "none";
                node.remove();
            };
            this.show = function() {
                node.style.display = "block";
            };

            // 先移除 click 事件,再重新绑定
            closeBtn.removeEventListener("click", this.hide);
            closeBtn.addEventListener("click", this.hide);

        }

        // 调用函数生成,默认显示
        var modalSample = new modal({
            title: "模态框",
            content: "使用函数初始化!",
            width: 800,
            height: 500
        });
	</script>
</body>
</html>